<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <h2 class="login-title">供应商</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" class="login-button">登录</el-button>
          <el-button @click="handleReset" class="reset-button">重置</el-button>
        </el-form-item>
        <div class="register-link">
          <span>还没有账号？</span>
          <el-button type="text" @click="showRegisterDialog = true" class="register-button">立即注册</el-button>
        </div>
      </el-form>
    </el-card>

    <!-- 注册弹窗 -->
    <el-dialog title="注册" :visible.sync="showRegisterDialog" width="50%">
      <el-form :model="registerForm" :rules="registerRules" ref="registerForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="账号" prop="username">
              <el-input v-model="registerForm.username" placeholder="请输入账号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="nickname">
              <el-input v-model="registerForm.nickname" placeholder="请输入供应商名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="registerForm.mobile" placeholder="请输入手机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业资质" prop="qyzz">
              <el-input v-model="registerForm.qyzz" placeholder="请输入企业资质"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附件" prop="fj">
              <el-upload :action="uploadUrl" list-type="text" :on-success="handleUploadSuccess"
                :on-remove="handleUploadRemove" :file-list="registerForm.fj">
                <el-button type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">支持单个或多个文件上传</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showRegisterDialog = false">取消</el-button>
        <el-button type="primary" @click="handleRegister">注册</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addGys } from '@/api/auth'; // 假设你有一个创建角色的 API
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      showRegisterDialog: false, // 控制注册弹窗的显示
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        nickname: '',
        mobile: '',
        qyzz: '',
        fj: [] // 附件字段改为数组
      },
      // 上传地址
      uploadUrl: `${import.meta.env.VITE_APP_BASE_API}/file/upload?source=album-cover`,
      registerRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value !== this.registerForm.password) {
                callback(new Error('两次输入的密码不一致'));
              } else {
                callback();
              }
            },
            trigger: 'blur'
          }
        ],
        nickname: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }],
        qyzz: [{ required: true, message: '请输入企业资质', trigger: 'blur' }],
        fj: [{ required: true, message: '请输入附件', trigger: 'blur' }]
      }
    };
  },
  methods: {
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          try {
            await this.$store.dispatch('loginAction', this.loginForm);
            this.$message.success('登录成功');
            this.$router.push({ path: '/home' });
          } catch (error) {
            this.$message.error(error.message || '登录失败，请重试');
          }
        } else {
          this.$message.error('请填写完整的登录信息');
        }
      });
    },
    handleReset() {
      this.$refs.loginForm.resetFields();
    },
    async handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          try {
            const obj = {
              roleIds:[20],
               user:{
            ...this.registerForm,
                 fj: this.registerForm.fj.join(','), // 将附件数组转换为逗号分隔的字符串
            }
            };
            await addGys(obj);
            this.$message.success('已提交注册申请，请等待审核')
            this.showRegisterDialog = false;
            this.resetRegisterForm();
          } catch (error) {
            this.$message.error(error.message || '注册失败，请重试');
          }
        } else {
          this.$message.error('请填写完整的注册信息');
        }
      });
    },
    handleUploadSuccess(response, file, fileList) {
      this.registerForm.fj = fileList.map(item => item.response?.data || item.url);
      this.$message.success('文件上传成功');
    },
    handleUploadRemove(file, fileList) {
      this.registerForm.fj = fileList.map(item => item.response?.data || item.url);
      this.$message.info('文件已移除');
    },
    resetRegisterForm() {
      this.registerForm = {
        username: '',
        password: '',
        confirmPassword: '',
        nickname: '',
        mobile: '',
        qyzz: '',
        fj: [] // 重置为数组
      };
    }
  }
};
</script>

<style scoped>
/* 登录页面整体容器 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 视口高度 */
  background-color: #f0f2f5;
}

/* 登录卡片 */
.login-card {
  width: 450px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

/* 登录标题 */
.login-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

/* 登录按钮 */
.login-button {
  margin-right: 10px;
  width: 100px;
  background-color: #409eff;
  color: #fff;
  border-radius: 4px;
  transition: all 0.3s;
}

.login-button:hover {
  background-color: #66b1ff;
  color: #fff;
}

/* 重置按钮 */
.reset-button {
  width: 100px;
  border-radius: 4px;
  transition: all 0.3s;
}

.reset-button:hover {
  background-color: #f0f0f0;
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-top: 10px;
  font-size: 14px;
  color: #606266;
}

.register-button {
  color: #409eff;
  font-weight: bold;
  padding: 0;
  font-size: 14px;
  transition: all 0.3s;
}

.register-button:hover {
  color: #66b1ff;
  text-decoration: underline;
}

/* 注册弹窗 */
.el-dialog {
  border-radius: 8px;
}

.dialog-footer {
  text-align: right;
}

/* 上传组件提示文字 */
.el-upload__tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}

/* 表单项间距 */
.el-form-item {
  margin-bottom: 20px;
}

/* 表单标题 */
.el-form-item__label {
  font-weight: bold;
  color: #606266;
}

/* 上传按钮 */
.el-upload .el-button {
  background-color: #409eff;
  color: #fff;
  border-radius: 4px;
  transition: all 0.3s;
}

.el-upload .el-button:hover {
  background-color: #66b1ff;
  color: #fff;
}
</style>
